<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/5
 * Time: 11:27
 */
namespace app\wechat\controller;
use app\models\Bases;
use app\service\Sms;
use think\Request;

class Person extends Base{
    public function person(){
        $openid = cookie('openid');
        $user_info = self::getUserInfo("openid = '{$openid}'",'nickname,openid,avatar,validity,username,uid');
        return view('person',['user_info'=>$user_info]);
    }
    /**
     * @return \think\response\View
     * 修改昵称
     */
    public function nickname(){
        try{
           if(Request::instance()->isPost()){
               $openid = cookie('openid');
               $user = new Bases('user');
               $post = input('param.');
               if($user->setField("openid = '{$openid}'",$post)===false){
                   throw new \LogicException('操作失败',10028);
               }
               return json(self::formatSuccessResult());
           }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return view('nickname');
    }

    /**
     *更新用户头像
     */
    public function saveAvatar(){
        try{
            $openid = cookie('openid');
            $user = new Bases('user');
            $file = request()->file('image');
            if(!$file)
                throw new \LogicException($file->getError(),10021);
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'avatar');// 移动到框架应用根目录/public/uploads/chat 目录下
            if(!$info)
                throw new \LogicException('创建目录失败',10022);
            $avatar_path = DS.'uploads'.DS.'avatar' .DS.$info->getSaveName();;
            if($user->setField("openid = '{$openid}'",['avatar'=>$avatar_path]) === false){
                throw new \LogicException('操作失败',10023);
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult($avatar_path));
    }

    /**
     * @return \think\response\View
     * 重置密码
     */
    public function password(){
        $userModel = new Bases('user');
        try{
            $openid = cookie('openid');
            if(empty($openid)){
                throw new \LogicException('cookie失效',10024);
            }
            if(request()->isPost()){
                $post = input('param.');
                if(empty(trim($post['password'])) || empty(trim($post['newpassword'])) ||empty(trim($post['repeatpassword'])) ||empty(trim($post['verificationCode']))){
                    throw new \LogicException('缺少参数',10020);
                }
                $userOne = self::getUserInfo("openid = '{$openid}'",'password,username');
                if(empty($userOne['password'])){
                    throw new \LogicException('请先绑定手机',1001);
                }
                if(!password_verify($post['password'],$userOne['password'])){
                    throw new \LogicException('原密码不正确',1002);
                }
                if( trim($post['newpassword']) != trim($post['repeatpassword'])){
                    throw new \LogicException('两次密码不一致',1003);
                }
                if(password_verify($post['newpassword'],$userOne['password'])){
                   throw new \LogicException('新密码不能和原密码一样',1004);
                }
                 //验证验证码
                $code = cookie($userOne['username'].'resetPassword');
                if(empty($code) || $code != trim($post['verificationCode'])){
                    throw new \LogicException('验证码不正确',1006);
                }
                $rs = $userModel->setField(" openid = '{$openid}' ",['password'=>password_hash($post['newpassword'],true)]);
                if($rs === false){
                    throw new \LogicException('操作失败',1005);
                }
                return json(self::formatSuccessResult());
            }
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        $result = $userModel->find("openid = '{$openid}'");
        return view('password',['result'=>$result]);
    }
    /**
     * 发送验证 resetPassword
     */
    public function sendcode(){
        $sms = new Sms();
        try{
            if(!request()->isPost()){
               throw new \LogicException('请求凡是不正确',10010);
            }
            $openid = cookie('openid');
            $userOne= self::getUserInfo("openid = '{$openid}' ",'*');
            if(empty($userOne['username'])){
                throw new \LogicException('请先绑定手机',1001);
            }
            $data['action'] = 'resetPassword';
            $data['username']= $userOne['username'];
            $result = $sms->sendCode($data);

        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json($result);
    }
}